package com.team.databoard.util;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ReadConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.team.databoard.common.ErrorCode;
import com.team.databoard.constant.StudentConstant.*;
import com.team.databoard.exception.BusinessException;
import com.team.databoard.mapper.StudentMapper;
import com.team.databoard.model.Student;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


public class DatabaseUtils {
    /**
     * 根据某一字段分组计数统计
     *
     * @param studentMapper studentMapper
     * @param column        字段名
     * @param provinceId    筛选指定省份id
     * @param contains      必须包含的键
     * @return 数据与计数的映射Map
     */
    public static Map<String, Integer> groupBy(StudentMapper studentMapper, String column, Integer provinceId, List<String> contains) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        if (provinceId != null) {
            if (EnumUtils.isIncludeCode(Province.class, provinceId)) {
                queryWrapper.eq("province", provinceId);
            } else {
                throw new BusinessException(ErrorCode.PARAMS_ERROR);
            }
        }
        queryWrapper.select(column + " as item, count(*) as count").groupBy("item");
        Map<String, Integer> result = studentMapper.selectMaps(queryWrapper).stream()
                .collect(Collectors.toMap(
                        map -> map.get("item").toString(),
                        map -> ((Long) map.get("count")).intValue()
                ));
        if (contains != null) {
            for (String key : contains) {
                if (!result.containsKey(key)) {
                    result.put(key, 0);
                }
            }
        }
        return result;
    }

    /**
     * 根据某一字段分组计数统计
     *
     * @param studentMapper studentMapper
     * @param column        字段名
     * @param provinceId    筛选指定省份id
     * @return 数据与计数的映射Map
     */
    public static Map<String, Integer> groupBy(StudentMapper studentMapper, String column, Integer provinceId) {
        return groupBy(studentMapper, column, provinceId, null);
    }

    /**
     * 将性别转换为int工具类
     *
     * @author guojing2003
     */
    public static class SexConverter implements Converter<Integer> {
        @Override
        public Class<?> supportJavaTypeKey() {
            return Integer.class;
        }

        @Override
        public CellDataTypeEnum supportExcelTypeKey() {
            return CellDataTypeEnum.STRING;
        }

        @Override
        public Integer convertToJavaData(ReadConverterContext<?> context) {
            String value = context.getReadCellData().getStringValue();
            return convertSexToInt(value);
        }
    }

    /**
     * 将省份转换为int工具类
     *
     * @author guojing2003
     */
    public static class ProvinceConverter implements Converter<Integer> {
        @Override
        public Class<?> supportJavaTypeKey() {
            return Integer.class;
        }

        @Override
        public CellDataTypeEnum supportExcelTypeKey() {
            return CellDataTypeEnum.STRING;
        }

        @Override
        public Integer convertToJavaData(ReadConverterContext<?> context) {
            String value = context.getReadCellData().getStringValue();
            return convertProvinceToInt(value);
        }
    }

    /**
     * 将学历转换为int工具类
     *
     * @author guojing2003
     */
    public static class EducationConverter implements Converter<Integer> {
        @Override
        public Class<?> supportJavaTypeKey() {
            return Integer.class;
        }

        @Override
        public CellDataTypeEnum supportExcelTypeKey() {
            return CellDataTypeEnum.STRING;
        }

        @Override
        public Integer convertToJavaData(ReadConverterContext<?> context) {
            String value = context.getReadCellData().getStringValue();
            return convertEducationToInt(value);
        }
    }

    /**
     * 将性别转换为int
     *
     * @param sex 性别
     * @return 0-男 1-女 (-1)-错误
     */
    static int convertSexToInt(String sex) {
        for (Sex item : Sex.values()) {
            if (item.getName().equals(sex)) {
                return item.getCode();
            }
        }
        return -1;
    }

    /**
     * 将省份转换为int
     *
     * @param province 省份名
     * @return 省份代码 (-1)-错误
     */
    static int convertProvinceToInt(String province) {
        for (Province item : Province.values()) {
            if (item.getName().equals(province) || item.getShortName().equals(province)) {
                return item.getCode();
            }
        }
        return -1;
    }

    /**
     * 将学历转换为int
     *
     * @param education 学历
     * @return 0-本科 1-硕士 2-博士 (-1)-错误
     */
    static int convertEducationToInt(String education) {
        if (education.endsWith("生")) {
            education = education.substring(0, education.length() - 1);
        }
        for (Education item : Education.values()) {
            if (item.getName().equals(education)) {
                return item.getCode();
            }
            for (String alias : item.getAlias()) {
                if (alias.equals(education)) {
                    return item.getCode();
                }
            }
        }
        return -1;
    }
}
